import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Server } from 'socket.io'
import { RedisService } from '../redis/RedisService'
import { RoomStateService } from '../room/RoomStateService'

@Injectable()
export class StreamingService {
  constructor(
    private readonly configService: ConfigService,
    private readonly redisService: RedisService,
    private readonly roomStateService: RoomStateService,
    private readonly io: Server
  ) {}

  // 直播流管理
  async startStream(roomId: string, streamUrl: string) {
    await this.roomStateService.updateRoomState(roomId, {
      streaming: true,
      streamUrl,
    })
    this.io.to(roomId).emit('stream-started', { streamUrl })
  }

  async stopStream(roomId: string) {
    await this.roomStateService.updateRoomState(roomId, {
      streaming: false,
      streamUrl: null,
    })
    this.io.to(roomId).emit('stream-stopped')
  }

  async getStreamStatus(roomId: string) {
    return this.roomStateService.getRoomState(roomId)
  }
}
